#include "unp.h"

int main(int argc, char** argv) {
	if(argc != 2) {
		err_msg("Usage:%s dest_ip", argv[0]);
		return -1;
	}

	char buf[MAXLINE];
	int fd;
	struct sockaddr_in dest_addr;
	if((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
		err_exit("socket error");
	memset(&dest_addr, 0, sizeof(dest_addr));
	dest_addr.sin_family = AF_INET;
	dest_addr.sin_port = htons(9987);
	if(inet_pton(AF_INET, argv[1],  &dest_addr.sin_addr) != 1) {
		err_exit("inet_pton:%s error.", argv[1]);
	}

	connect(fd,(struct sockaddr*)&dest_addr, sizeof(dest_addr));

	int n = 0;
	while(fgets(buf, MAXLINE, stdin) != NULL) {
		if((n = Write(fd, buf, strlen(buf))) == -1) {
			err_exit("sendto error.");
		}
		if((n = Read(fd, buf, strlen(buf))) == -1) {
			err_exit("recv error.");
		}
		buf[n] = 0;
		fputs(buf, stdout);
	}


	return 0;
}
